package com.example.myapplication;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class ChannelAdapter extends BaseAdapter {

    private ArrayList<Channel> channelList;
    private LayoutInflater layoutInflater;

    public ChannelAdapter(ArrayList<Channel> list, Context context)
    {
        channelList = list;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount()
    {
        return channelList.size();
    }
    @Override
    public Object getItem(int position)
    {
        return channelList.get(position);
    }
    @Override
    public long getItemId(int position)
   {
       return 0;
   }
   @Override
   public View getView(int position , View convertView, ViewGroup parent)
   {
       ViewHolder holder = null;
       if(convertView == null)
       {
           convertView = layoutInflater.inflate(R.layout.grid_item,null);

           holder = new ViewHolder();
           holder.imgChannel = (ImageView)convertView.findViewById(R.id.channel_img);
           holder.decChannel = (TextView)convertView.findViewById(R.id.channel_dec);
           convertView.setTag(holder);
       }
       else
       {
           holder = (ViewHolder)convertView.getTag();
       }

       Channel channel = channelList.get(position);
       if (channel != null)
       {
           holder.decChannel.setText(channel.getDec());
           switch (channel.getDec())
           {
               case "永劫无间":
                   holder.imgChannel.setImageResource(R.drawable.yjtb);
                   break;
               case "彩虹六号":
                   holder.imgChannel.setImageResource(R.drawable.r6tb);
                   break;
               case "怪物猎人":
                   holder.imgChannel.setImageResource(R.drawable.gwlrtb);
                   break;
               case "尼尔机械纪元":
                   holder.imgChannel.setImageResource(R.drawable.nrtb);
                   break;
               case "鬼泣五":
                   holder.imgChannel.setImageResource(R.drawable.gqtb);
                   break;
               case "PUBG":
                   holder.imgChannel.setImageResource(R.drawable.pubg);
                   break;

           }
       }
       return convertView;
   }

   class ViewHolder
   {
       ImageView imgChannel;
       TextView decChannel;
   }
}
